package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.THomeworkStu;
import com.ruoyi.business.domain.TworkStuComment;
import com.ruoyi.business.domain.TworkStuCommentDetail;
import com.ruoyi.business.domain.bo.FinishedHomeWorkBo;
import com.ruoyi.business.domain.bo.GroupCommentBo;
import com.ruoyi.business.domain.vo.StuOtherCommentVo;
import com.ruoyi.business.mapper.THomeworkStuMapper;
import com.ruoyi.business.mapper.TworkStuCommentDetailMapper;
import com.ruoyi.business.mapper.TworkStuCommentMapper;
import com.ruoyi.business.service.ITworkStuCommentService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 作业互评主Service业务层处理
 * 
 * @author 最爱喝兽奶
 * @date 2022-08-14
 */
@SuppressWarnings("ALL")
@Service
public class TworkStuCommentServiceImpl implements ITworkStuCommentService
{
    @Autowired
    private TworkStuCommentMapper tworkStuCommentMapper;

    @Autowired
    private THomeworkStuMapper homeworkStuMapper;

    @Autowired
    private TworkStuCommentDetailMapper tworkStuCommentDetailMapper;

    /**
     * 查询作业互评主
     * 
     * @param id 作业互评主主键
     * @return 作业互评主
     */
    @Override
    public TworkStuComment selectTworkStuCommentById(Long id)
    {
        return tworkStuCommentMapper.selectTworkStuCommentById(id);
    }

    /**
     * 查询作业互评主列表
     * 
     * @param tworkStuComment 作业互评主
     * @return 作业互评主
     */
    @Override
    public List<TworkStuComment> selectTworkStuCommentList(TworkStuComment tworkStuComment)
    {
        return tworkStuCommentMapper.selectTworkStuCommentList(tworkStuComment);
    }

    /**
     * 新增作业互评主
     * 
     * @param tworkStuComment 作业互评主
     * @return 结果
     */
    @Override
    public int insertTworkStuComment(TworkStuComment tworkStuComment)
    {
        return tworkStuCommentMapper.insertTworkStuComment(tworkStuComment);
    }

    /**
     * 修改作业互评主
     * 
     * @param tworkStuComment 作业互评主
     * @return 结果
     */
    @Override
    public int updateTworkStuComment(TworkStuComment tworkStuComment)
    {
        return tworkStuCommentMapper.updateTworkStuComment(tworkStuComment);
    }

    /**
     * 批量删除作业互评主
     * 
     * @param ids 需要删除的作业互评主主键
     * @return 结果
     */
    @Override
    public int deleteTworkStuCommentByIds(Long[] ids)
    {
        return tworkStuCommentMapper.deleteTworkStuCommentByIds(ids);
    }

    /**
     * 删除作业互评主信息
     * 
     * @param id 作业互评主主键
     * @return 结果
     */
    @Override
    public int deleteTworkStuCommentById(Long id)
    {
        return tworkStuCommentMapper.deleteTworkStuCommentById(id);
    }

    @Override
    public List<StuOtherCommentVo> groupOtherComment(GroupCommentBo bo) {
        List<StuOtherCommentVo> vo = new ArrayList<>();
        //每个人要互评作业数
        int number = 5;
        if (StringUtils.isNotNull(bo.getNumber()) && StringUtils.isNotEmpty(bo.getNumber())) {
            number = Integer.parseInt(bo.getNumber());
        }
        THomeworkStu t1 = new THomeworkStu();
        t1.setCourseId(bo.getCourseId());
        t1.setFinished("1");
        //这个课程下一共有几份作业
        List<THomeworkStu> list = homeworkStuMapper.selectTHomeworkStuByCourseIdAndUid(t1);


        //这个课程下一共有多少人
        THomeworkStu t2 = new THomeworkStu();
        t2.setCourseId(bo.getCourseId());
        List<THomeworkStu> list2 = homeworkStuMapper.selectTHomeworkStuByCourseIdAndUid(t2);


        int size = list.size();
        if (size == 0) {
            return vo;
        }
        if (number >= (size-1)) {
            number = (size-1);
        }

        int finalNumber = number;
        list2.forEach(e -> {
              String commentUser = e.getUploadUser();

              //把评价人去掉
              List<THomeworkStu> needCommentList = list2.stream()
                      .filter(d -> !(d.getUploadUser()).equals(commentUser))
                      .collect(Collectors.toList());

            for (int i = 0; i < finalNumber; i++) {
                int offset = (int) (Math.floor(Math.random() * needCommentList.size()));
                THomeworkStu homeworkStu = needCommentList.get(offset);
                TworkStuComment tworkStuComment = tworkStuCommentMapper.selectByWorkId(String.valueOf(homeworkStu.getId()));
                tworkStuCommentDetailMapper.deleteByCommentId(String.valueOf(tworkStuComment.getId()));
                TworkStuCommentDetail detail = new TworkStuCommentDetail();
                detail.setCommentId(String.valueOf(tworkStuComment.getId()));
                detail.setCommentUser(commentUser);
                detail.setFinished("0");
                tworkStuCommentDetailMapper.insertTworkStuCommentDetail(detail);
            }


        });

        FinishedHomeWorkBo bo2 = new FinishedHomeWorkBo();
        bo2.setCourseId(bo.getCourseId());
        List<StuOtherCommentVo> stuOtherCommentVos = tworkStuCommentMapper.selectOthCommentDetailByCourseIdAndUid(bo2);


        return stuOtherCommentVos;
    }

    @Override
    public List<StuOtherCommentVo> getAllCommentDetail( FinishedHomeWorkBo bo) {

        List<StuOtherCommentVo> stuOtherCommentVos = tworkStuCommentMapper.selectOthCommentDetailByCourseIdAndUid(bo);

        return stuOtherCommentVos;
    }

    @Override
    public int teacherComment(THomeworkStu tHomeworkStu) {
        List<THomeworkStu> list = homeworkStuMapper.selectTHomeworkStuByCourseIdAndUid(tHomeworkStu);
        if (list.size() > 0) {
            tHomeworkStu.setId( list.get(0).getId());
        }
        return homeworkStuMapper.updateTHomeworkStu(tHomeworkStu);

    }

    @Override
    public int studentComment(TworkStuCommentDetail tworkStuCommentDetail) {
        String score = tworkStuCommentDetail.getScore();
        String commentTime = tworkStuCommentDetail.getCommentTime();
        String finished = tworkStuCommentDetail.getFinished();
        tworkStuCommentDetail.setScore(null);
        tworkStuCommentDetail.setFinished(null);
        tworkStuCommentDetail.setCommentTime(null);
        List<TworkStuCommentDetail> tworkStuCommentDetails = tworkStuCommentDetailMapper.selectTworkStuCommentDetailList(tworkStuCommentDetail);
        if (tworkStuCommentDetails.size() > 0) {
            tworkStuCommentDetail.setId(tworkStuCommentDetails.get(0).getId());
        }
        tworkStuCommentDetail.setScore(score);
        tworkStuCommentDetail.setFinished(finished);
        tworkStuCommentDetail.setCommentTime(commentTime);
        return tworkStuCommentDetailMapper.updateTworkStuCommentDetail(tworkStuCommentDetail);
    }
}
